<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Authentication</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: auto;
            padding: 20px;
        }
        h2 {
            text-align: center;
        }
        form {
            margin: 20px 0;
        }
        input[type="text"], input[type="password"], input[type="email"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            padding: 10px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .message {
            text-align: center;
            color: #333;
        }
    </style>
</head>
<body>

<h2>Register</h2>
<form id="registerForm">
    <input type="text" id="registerUsername" placeholder="Username" required>
    <input type="email" id="registerEmail" placeholder="Email" required>
    <input type="password" id="registerPassword" placeholder="Password" required>
    <button type="submit">Register</button>
</form>
<div id="registerMessage" class="message"></div>

<h2>Login</h2>
<form id="loginForm">
    <input type="text" id="loginUsername" placeholder="Username" required>
    <input type="password" id="loginPassword" placeholder="Password" required>
    <button type="submit">Login</button>
</form>
<div id="loginMessage" class="message"></div>

<script>
    // 注册处理
    document.getElementById('registerForm').addEventListener('submit', function(event) {
        event.preventDefault();

        const username = document.getElementById('registerUsername').value;
        const email = document.getElementById('registerEmail').value;
        const password = document.getElementById('registerPassword').value;

        const registerData = {
            username: username,
            email: email,
            password: password
        };

        fetch('http://localhost:8080/api/auth/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(registerData)
        })
            .then(response => response.json())
            .then(data => {
                document.getElementById('registerMessage').textContent = data.message || "User registered successfully!";
            })
            .catch(error => {
                document.getElementById('registerMessage').textContent = "Error registering user!";
                console.error('Error:', error);
            });
    });

    // 登录处理
    document.getElementById('loginForm').addEventListener('submit', function(event) {
        event.preventDefault();

        const username = document.getElementById('loginUsername').value;
        const password = document.getElementById('loginPassword').value;

        const loginData = {
            username: username,
            password: password
        };

        fetch('http://localhost:8080/api/auth/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(loginData)
        })
            .then(response => response.json())
            .then(data => {
                document.getElementById('loginMessage').textContent = data.message || "Login successful!";
            })
            .catch(error => {
                document.getElementById('loginMessage').textContent = "Invalid credentials!";
                console.error('Error:', error);
            });
    });
</script>

</body>
</html>
